/*判断一个字符串是否是由其子串重复组成*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool repeatSubStr(const char *str) {
    int n = strlen(str);
    // 遍历可能的子串长度，从 1 到 n / 2
    for (int len = 1; len <= n / 2; len++) 
    {
        // 检查是否能通过重复子串构成原字符串
        if (n % len == 0) {  
            // 子串长度必须能整除字符串长度
            bool isValid = true;
            //滑动窗口：比较i和i%len字符是否相等，不相等就为false
            for (int i = len; i < n; i++)
             {
                if (str[i] != str[i % len]) 
                {
                    isValid = false;
                    break;
                }
            }
            if (isValid) 
            {
                return true;
            }
        }
    }

    return false;
}

int main()
 {
    const char *str = "ababab";
    if (repeatSubStr(str)) {
        printf("The string is constructed by repeating a substring.\n");
    } else {
        printf("The string is not constructed by repeating a substring.\n");
    }

    return 0;
}